#include <ncurses.h>
#include <string>
using namespace std;

// do play game

struct Player {
Player(WINDOW *win, int y, int x, char c);

void mvup();
void mvdown();
void mvleft();
void mvright();
int getmv();
void display();

	private:
int xloc, yloc, xmax, ymax;
char character;
WINDOW* curwin;
};

Player::Player(WINDOW* win, int y, int x, char c)
{
	curwin = win;
	yloc = y;
	xloc = x;
	getmaxyx(curwin, ymax, xmax);
	keypad(curwin, true);
	character = c;
}

void Player::mvup()
{
	mvwaddch(curwin, yloc, xloc, '.');
	yloc--;
	if (yloc < 1)
		yloc = 1;
}

void Player::mvdown()
{
	mvwaddch(curwin, yloc, xloc, '.');
	yloc++;
	if (yloc > ymax - 2)
		yloc = ymax - 2;
}

void Player::mvleft()
{
	mvwaddch(curwin, yloc, xloc, '.');
	xloc--;
	if (xloc < 1)
		xloc = 1;
}

void Player::mvright()
{
	mvwaddch(curwin, yloc, xloc, '.');
	xloc++;
	if (xloc > xmax-2)
		xloc = xmax -2;
}

int Player::getmv()
{
	int choice = wgetch(curwin);
	switch(choice) {
		case KEY_UP:
			mvup();
			break;
		case KEY_DOWN:
			mvdown();
			break;
		case KEY_LEFT:
			mvleft();
			break;
		case KEY_RIGHT:
			mvright();
			break;
		default:
			break;
	}
	return choice;
}

void Player::display()
{
	mvwaddch(curwin, yloc, xloc, character);
}

int main(int argc , char** argv)
{
	initscr();
	noecho();
	cbreak();

	int y, x, yBeg, xBeg, yMax, xMax;
	getmaxyx(stdscr, yMax, xMax);
	printw("x to escape this game");

	WINDOW* win = newwin(20, 50, (yMax/2) - 10, 10);
	box(win, 0, 0);
	refresh();
	wrefresh(win);

	Player *p = new Player(win, 1, 1, '@');
	do {
		p->display();
		wrefresh(win);
	} while (p->getmv() != 'x') ;

	getch();

	endwin();
	

	return 0;
}
